<?php
/**
 *
 * @author    hequan
 * @date       2012-09-05
 * @todo      队列日志
 *
 */

class QueueLog
{
    // 日志文件句柄数组
    private static $fd_array = array(
        'debug'       => array(        // 1
                        'fd'	=> 0,
                        'date_cursor'	=> "",
                        ),
        'business'    => array(        // 2
                        'fd'	=> 0,
                        'date_cursor'	=> "",
                        ),
        'error'       => array(        // 3
                        'fd'	=> 0,
                        'date_cursor'	=> "",
                        ),
        'sqlerror'    => array(        // 4
                        'fd'	=> 0,
                        'date_cursor'	=> "",
                        ),
    );

	public static $queue_name = 'default';
	public static function setQueueName($name) {
		return self::$queue_name = $name;
	}

	public static function noticeLog($str, $file = '')
	{
		echo '[' .date('Y-m-d H:i:s').'] ';
		if (is_array($str)) {
			var_dump($str);
		} else {
			echo $str;
		}
		echo "\n";
	}

	public static function debugLog($str, $file = '') {
		self::noticeLog($str);
// 		return self::write($str, 1, $file);
		return true;
	}

	public static function businessLog($str, $file = '') {
	    self::noticeLog($str);
		return self::write($str, 2, $file);
	}

	public static function errorLog($str, $file = '') {
		self::noticeLog($str);
		return self::write($str, 3, $file);
	}

	public static function sqlErrorLog($str,$file = '') {
		self::noticeLog($str);
		return self::write($str, 4, $file);
	}

    static function write($message, $type=1, $file='')
    {
        $file_path = "";
        $task_name = "";
        switch($type)
        {
            case 1:
                // debug
                $file_path = realpath(QUEUE_ROOT_DIR).'/log/debug/'.date('Y-m')."/";
                $task_name = 'debug';
                break;
            case 2:
                // business
                $file_path = realpath(QUEUE_ROOT_DIR).'/log/business/'.date('Y-m')."/";
                $task_name = 'business';
                break;
            case 3:
            	$file_path = realpath(QUEUE_ROOT_DIR).'/log/error/'.date('Y-m')."/";
            	$task_name = 'error';
                break;
            case 4:
                // sqlerror
                $file_path = realpath(QUEUE_ROOT_DIR).'/log/sqlerror/'.date('Y-m')."/";
                $task_name = 'sqlerror';
                break;
            default :
            	$file_path = realpath(QUEUE_ROOT_DIR).'/log/debug/'.date('Y-m')."/";
            	$task_name = 'debug';
                break;
        }

    	if (!file_exists($file_path)) {
			@mkdir($file_path, 0777, 1);
		}

        $file_name = $file ? $file.'_'.date('Y-m-d').'.log' : ( self::$queue_name .'_'. date('Y-m-d') ).'.log';
        if(!is_writable($file_path)){
           echo ($file_path.$file_name . " LOG_FILE_NOT_WRITEABLE_ \n");
           return ;
        }

        if(!isset(self::$fd_array[$task_name]) || self::$fd_array[$task_name]['fd'] == 0 || date('Y-m-d') != self::$fd_array[$task_name]['date_cursor'])
        {
            if(!isset(self::$fd_array[$task_name]) || self::$fd_array[$task_name]['fd'] != 0)
            {
            	if(isset(self::$fd_array[$task_name])){
                	fclose(self::$fd_array[$task_name]['fd']);
            	}

                self::$fd_array[$task_name]['fd'] = 0;
            }

            if((self::$fd_array[$task_name]['fd'] = fopen($file_path.$file_name, 'a+')) === false)
            {
                self::$fd_array[$task_name]['fd'] = 0;
                echo "fopen {$file_path}{$file_name}\n";
                return;
            }

            self::$fd_array[$task_name]['date_cursor'] = date('Y-m-d');
        }

        fwrite(self::$fd_array[$task_name]['fd'], '[' .date('Y-m-d H:i:s ').microtime(true).'] '."$message\n");
        fflush(self::$fd_array[$task_name]['fd']);	// 开发测试用
    }
}